package com.changwu.cwbase.ui.activity.main;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.changwu.cwbase.Constants;
import com.changwu.cwbase.R;
import com.changwu.cwbase.base.ActivityBase;
import com.changwu.cwbase.bean.entity.User;
import com.changwu.cwbase.bean.response.ListResponse;
import com.changwu.cwbase.net.ApiM;
import com.changwu.cwbase.net.RxObserver;
import com.changwu.cwbase.net.RxSchedulers;
import com.changwu.cwbase.ui.adapter.CardAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/*
 * RecyclerView结合CardView
 */
public class RecyclerCardViewActivity extends ActivityBase {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private CardAdapter adapter;
    private List<User> beans = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recylerview);
        initData();
        initView();
    }

    private void initData() {
        User user = null;
        for (int i = 0; i < 5; i++) {
            user = new User();
            beans.add(user);
        }
    }

    @Override
    public void initToolBar() {
        super.initToolBar();
        title.setText("RecyclerView结合CardView");
    }

    private void initView() {
        adapter = new CardAdapter();
        recyclerView.setNestedScrollingEnabled(false);
        recyclerView.setLayoutManager(new LinearLayoutManager(activity));
        recyclerView.setAdapter(adapter);
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.tvName:
                        break;
                }

            }
        });
        adapter.setNewData(beans);
//        req();
    }


    private void req() {
        JSONObject params = new JSONObject();
        params.put("uid", "");
        ApiM.getInstance().getUserService().getList(params).compose(RxSchedulers.<ListResponse>io_main()).subscribeWith(new RxObserver<ListResponse>(TAG, 0) {
            @Override
            public void success(ListResponse response) {
                if (response.errno == Constants.success) {
                    beans = response.data;
                    adapter.setNewData(beans);
                } else {
                    ToastUtils.showShort(response.errmsg);
                }
            }
        });
    }
}
